/* Team Directories
 *
 * You are free to do whatever you want with this code. Frankly we don't give a damn.
 * 
 *  
 * Copyright GNU General Public License v3
 */


package StudyBuddy;

import javax.swing.*;

/**
 * Login user interface, with which the user will register and login to an 
 * account.
 * 
 * @version     1.10    15 Feb 2008
 * @author      Rajpreet Bajwa  
 */
public class LoginFrame extends javax.swing.JFrame {
    
    /** Creates new form LoginFrame */
    public LoginFrame() {
        initComponents();
    }
  
    /** create a new instance of the Login class */
    Login  newLogin = new Login();
    
    /** to store the username entered by the user */
    String name = null ;
    
    /** to stroe the password entered by the user */
    String password = null;
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        label3 = new java.awt.Label();
        jtxtUsername = new java.awt.TextField();
        label2 = new java.awt.Label();
        jtxtPassword = new javax.swing.JPasswordField();
        label1 = new java.awt.Label();
        jbtnExit = new javax.swing.JButton();
        jbtnRegister = new javax.swing.JButton();
        jbtnLogin = new javax.swing.JButton();
        ErrorMsg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Study Buddy");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 40)));

        label3.setFont(new java.awt.Font("Dialog", 0, 18));
        label3.setText("Password");

        jtxtUsername.setFont(new java.awt.Font("Dialog", 0, 18));
        jtxtUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtxtUsernameFocusGained(evt);
            }
        });

        label2.setFont(new java.awt.Font("Dialog", 0, 18));
        label2.setText("Username");

        jtxtPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtxtPasswordFocusGained(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
                    .addComponent(jtxtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(72, 72, 72)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(75, 75, 75))
        );

        label1.setAlignment(java.awt.Label.CENTER);
        label1.setFont(new java.awt.Font("Dialog", 1, 48));
        label1.setText("Study Buddy");

        jbtnExit.setFont(new java.awt.Font("Tahoma", 1, 14));
        jbtnExit.setText("Exit");
        jbtnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExitActionPerformed(evt);
            }
        });

        jbtnRegister.setFont(new java.awt.Font("Tahoma", 1, 14));
        jbtnRegister.setText("Register");
        jbtnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRegisterActionPerformed(evt);
            }
        });

        jbtnLogin.setFont(new java.awt.Font("Tahoma", 1, 14));
        jbtnLogin.setText("Login");
        jbtnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLoginActionPerformed(evt);
            }
        });

        ErrorMsg.setFont(new java.awt.Font("Tahoma", 1, 24));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jbtnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 358, Short.MAX_VALUE)
                                .addComponent(jbtnRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(jbtnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(label1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 878, Short.MAX_VALUE)
                            .addComponent(ErrorMsg, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(51, 51, 51))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnRegister, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                    .addComponent(jbtnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(ErrorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );

        ErrorMsg.getAccessibleContext().setAccessibleName("error");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Button to exit the login screen.
     * @param evt
     */
    private void jbtnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExitActionPerformed

        System.exit(0); //close the login screen
    }//GEN-LAST:event_jbtnExitActionPerformed

    /**
     * Button to Register an account, with the information provided in the 
     * username text box and passwordt text box.  If the username entered has
     * already been registered, the user will be asked for a different username.
     * @param evt
     */
    private void jbtnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRegisterActionPerformed
    
        /**
         * get username from the textbox jtxtUsername and store the value in var
         * name of type String.
         */
        name = jtxtUsername.getText();  
        
         /**
         * get password from the password textbox jtxtpassword and store the 
         * value in var  password of type String.
         */
        password = jtxtPassword.getText();  
        
       /** call various functions from the Login class to Register the user */
        newLogin.getUsername(name); //call the function getUsername of Login
        newLogin.getPassword(password); 
        newLogin.checkUsernamePassowrd();
}//GEN-LAST:event_jbtnRegisterActionPerformed

    /**
     * Login a user, with the info provided in the textboxes for username and 
     * password.
     * @param evt
     */
    private void jbtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLoginActionPerformed

        boolean login = false;  //Boolean to store if a login was succesfull
        
        /**
         * get username from the textbox jtxtUsername and store the value in var 
         * name of type String.
         */
        name = jtxtUsername.getText();  
        
         /**
         * get password from the  password textbox jtxtpassword and store the 
         * value in the var password of type String.
         */
        password = jtxtPassword.getText();
        
        /** call various functions from the Login class to login(authenticate) the user */
        newLogin.getUsername(name);
        newLogin.getPassword(password);
        
        try{
        login = newLogin.validateUser();
        }
        catch (Exception e){
            
            ErrorMsg.setText("LOGIN/PASSWORD INCORRECT, PLEASE RETRY");
            
        }
        
       if (login == true)
       {
          /** create an instance of the class LoginInfo */
           LoginInfo newLoginInfo = new LoginInfo();    
           newLoginInfo.setUsernameInfo(name);  
           
           /** create an instrance of the class BuddyLoader */
           BuddyLoader MF = new BuddyLoader();
           MF.setVisible(true);
           this.setVisible(false);
           dispose();
           
           newLoginInfo.getUsernameInfo();
       }    //end if            
}//GEN-LAST:event_jbtnLoginActionPerformed

    private void jtxtPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtxtPasswordFocusGained
            ErrorMsg.setText("");
    }//GEN-LAST:event_jtxtPasswordFocusGained

    private void jtxtUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtxtUsernameFocusGained
            ErrorMsg.setText("");
    }//GEN-LAST:event_jtxtUsernameFocusGained
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ErrorMsg;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnExit;
    private javax.swing.JButton jbtnLogin;
    private javax.swing.JButton jbtnRegister;
    private javax.swing.JPasswordField jtxtPassword;
    private java.awt.TextField jtxtUsername;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    // End of variables declaration//GEN-END:variables
      
}   //LoginFrame
